import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;

/**
 * 超市
 *
 * @author fy
 * @date 2022/8/23 21:45
 */
public final class SuperMarket {

    /**
     * 商品列表
     */
    private Map<String, Fruit> fruits = new HashMap<>();

    /**
     * 活动价格策略
     */
    private PriceStrategy strategy = new DefaultStrategy();

    public SuperMarket(int caseType) {
        switch (caseType) {
            case 1:
                fruits.put("苹果", new Apple());
                fruits.put("草莓", new Strawberry());
                break;
            case 2:
                fruits.put("苹果", new Apple());
                fruits.put("草莓", new Strawberry());
                fruits.put("芒果", new Mango());
                break;
            case 3:
                Apple appleCase3 = new Apple();
                appleCase3.setStrategy(new DiscountStrategy(0.8d));
                fruits.put("苹果", appleCase3);
                fruits.put("草莓", new Strawberry());
                fruits.put("芒果", new Mango());
                break;
            case 4:
                Apple appleCase4 = new Apple();
                appleCase4.setStrategy(new DiscountStrategy(0.8d));
                fruits.put("苹果", appleCase4);
                fruits.put("草莓", new Strawberry());
                fruits.put("芒果", new Mango());
                this.strategy = new ReduceStrategy(100d, 10d);
                break;
        }
    }

    /**
     * 计算总价格
     *
     * @param goods 商品名和价格对应键值对
     * @return
     */
    public double calcPrice(Map<String, Double> goods) {
        System.out.println("------------购买清单--------------");
        double price = 0d;
        for (String fruitName : goods.keySet()) {
            Fruit fruit = fruits.get(fruitName);
            if (fruit == null) {
                throw new RuntimeException("错误的水果");
            }
            double weight = goods.get(fruitName);
            System.out.println(fruit + " 买了 " + weight + " 斤");
            double entryPrice = fruit.calcPrice(weight);
            price = BigDecimal.valueOf(price).add(BigDecimal.valueOf(entryPrice)).doubleValue();
        }
        double totalPrice = this.strategy.calcPrice(price);
        System.out.println("总价: " + totalPrice);
        return totalPrice;
    }

}
